<?php
/**
 * @author Ewan
 * @email 654846126@qq.com
 * The more effort ,the more lucky
 *      .--.
 *     |o_o |
 *     |:_/ |
 *    //   \ \
 *   (|     | )
 *  /'\_   _/`\
 *  \___)=(___/
 * @time: 17/3/10 下午4:47
 */

/**
 * pre print_r()
 */
if (!function_exists('p')) {
    function p($param)
    {
        echo '<pre>';
        print_r($param);
        echo '</pre>';
    }
}

/**
 * pre var_dump()
 */
if (!function_exists('vd')) {
    function vd($param)
    {
        echo '<pre>';
        var_dump($param);
        echo '<pre>';
    }
}

/**
 * @param null $time
 * @return int
 */
if (!function_exists('beginOfDay')) {
    function beginOfDay($time = null)
    {
        if (is_null($time)) {
            $time = time();
        }
        if (is_string($time) && !is_numeric($time)) {
            $time = strtotime($time);
        }
        return strtotime(date('Y-m-d 00:00:00', $time));
    }
}

/**
 * @param null $time
 * @return int
 */
if (!function_exists('endOfDay')) {
    function endOfDay($time = null)
    {
        if (is_null($time)) {
            $time = time();
        }
        if (is_string($time) && !is_numeric($time)) {
            $time = strtotime($time);
        }
        return strtotime(date('Y-m-d 23:59:59', $time));
    }
}

/**
 * @param null $time
 * @return int
 */
if (!function_exists('beginOfWeek')) {

    function beginOfWeek($time = null)
    {
        if (is_null($time)) {
            $time = time();
        }

        $day_number = date('W', $time) * 7;
        $week_day_number = date('w', mktime(0, 0, 0, 1, $day_number, date('Y', $time)));//星期中的第几天，数字表示
        $start_number = $day_number - $week_day_number;
        return mktime(0, 0, 0, 1, $start_number + 1, date('Y', $time));//开始日期
    }
}

/**
 * @param null $time
 * @return int
 */
if (!function_exists('endOfWeek')) {

    function endOfWeek($time = null)
    {
        if (is_null($time)) {
            $time = time();
        }

        $day_number = date('W', $time) * 7;

        $week_day_number = date('w', mktime(0, 0, 0, 1, $day_number, date('Y', $time)));//星期中的第几天，数字表示,周六

        $start_number = $day_number - $week_day_number;

        return mktime(23, 59, 59, 1, $start_number + 7, date('Y', $time));//结束日期
    }
}

/**
 * @param null $time
 * @return int
 */
if (!function_exists('beginOfMonth')) {

    function beginOfMonth($time = null)
    {
        if (is_null($time)) {
            $time = time();
        }
        return strtotime(date('Y-m-01 00:00:00', $time));
    }
}

/**
 * @param null $time
 * @return int
 */
if (!function_exists('endOfMonth')) {
    function endOfMonth($time = null)
    {
        if (is_null($time)) {
            $time = time();
        }
        return strtotime(date("Y-m-t 23:59:59", $time));
    }
}

/**
 * @param null $time
 * @return int
 */
if (!function_exists('beginOfYear')) {
    function beginOfYear($time = null)
    {

        if (is_null($time)) {
            $time = time();
        }
        return strtotime(date('Y-01-01 00:00:00', $time));
    }
}

/**
 * @param null $time
 * @return int
 */
if (!function_exists('endOfYear')) {
    function endOfYear($time = null)
    {

        if (is_null($time)) {
            $time = time();
        }
        return strtotime(date('Y-12-31 23:59:59', $time));
    }
}

/**
 * 二维数组去掉重复值
 * @param $array2D
 * @return array
 */
function array_unique_fb($array2D)
{
    foreach ($array2D as $v) {
        $v = join(',', $v);//降维,也可以用implode,将一维数组转换为用逗号连接的字符串
        $temp[] = $v;
    }
    $temp = array_unique($temp);//去掉重复的字符串,也就是重复的一维数组
    foreach ($temp as $k => $v) {
        $temp[$k] = explode(',', $v);//再将拆开的数组重新组装
    }
    return $temp;
}

/**
 * API接口返回JSON数据
 * @param int $code
 * @param string $msg
 * @param array $data
 */
function renderJSON($code, $msg = '', $data = [])
{
    $result = ['code' => $code, 'msg' => $msg];
    if ($data) {
        $result = array_merge($result, $data);
    }
    return $result;
}

// 获取经纬度
function getLocation($address)
{
    # 高德地图
    /*
    $ch = curl_init('http://restapi.amap.com/v3/geocode/geo?address=' . $address . '&output=json&key=0b00174f6f8ab4ca8d350ac0da105bb9');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
    $output = json_decode(curl_exec($ch), true);
    $location = explode(',', $output['geocodes'][0]['location']);
    */
    # 百度地图
    $address = trim($address);
    $ch = curl_init('http://api.map.baidu.com/geocoder/v2/?address=' . $address . '&output=json&ak=LGSF5bLoRGYzsOBrr20kXazopMHxLgL1');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
    $output = json_decode(curl_exec($ch), true);
    if (!$output) {
        return false;
    }
    $location = array_values($output['result']['location']);

    if (!$location) {
        return false;
    }
    return $location;
}

/**
 * 下拉筛选
 * @param $column
 * @param null $value
 * @return bool
 */
function dropDown($column, $value = null)
{
    $dropDownList = [
        #合同类型
        'type' => [
            1 => '软件合同',
            2 => '硬件合同',
            3 => '其他合同'
        ],
        # 有新的字段要实现下拉规则，可像上面这样进行添加
    ];
    # 根据具体值显示对应的值
    if ($value !== null) {
        return array_key_exists($column, $dropDownList) ? $dropDownList[$column][$value] : false;
    } else {
        # 返回关联数组，用户下拉的filter实现
        return array_key_exists($column, $dropDownList) ? $dropDownList[$column] : false;
    }
}

/**
 * 二维数组降维
 * @return array
 */
function array_dimensionality_reduction($array)
{
    $arr = [];
    foreach ($array as $k => $v) {
        foreach ($v as $item => $val) {
            if (!in_array($val, $arr)) {
                array_push($arr, $val);
            }
        }
    }
    return $arr;
}

function wLog($message, $cate, $file)
{
    echo $message . '========================' . $cate . "</br>\n";
    $time = microtime(true);
    $log = new \yii\log\FileTarget();
    $log->logFile = Yii::$app->getRuntimePath() . '/logs/' . $file . '.log'; //文件名自定义
    $log->messages[] = [$message, 1, $cate, $time];
    $log->export();
}

/**
 * 随机颜色
 * @return string
 */
function getColor()
{
    $str = '#';
    for ($i = 0; $i < 6; $i++) {
        $randNum = rand(0, 15);
        switch ($randNum) {
            case 10:
                $randNum = 'A';
                break;
            case 11:
                $randNum = 'B';
                break;
            case 12:
                $randNum = 'C';
                break;
            case 13:
                $randNum = 'D';
                break;
            case 14:
                $randNum = 'E';
                break;
            case 15:
                $randNum = 'F';
                break;
        }
        $str .= $randNum;
    }
    return $str;
}

/**
 *二维数组去重
 * @param $arr
 * @param $key
 * @return array
 */
//$arr->传入数组   $key->判断的key值
function array_unset_tt($arr, $key)
{
    //建立一个目标数组
    $res = array();
    foreach ($arr as $value) {
        //查看有没有重复项

        if (isset($res[$value[$key]])) {
            //有：销毁

            unset($value[$key]);

        } else {

            $res[$value[$key]] = $value;
        }
    }
    return $res;
}